package com.ws.base.common.datadict.support;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.ws.base.common.ContextHolder;
import com.ws.base.common.datadict.entity.Datadict;
import com.ws.base.common.datadict.service.DatadictService;

@Component
public class DatadictSupport {
    private DatadictSupport() {
    }

    private static class SingletonHolder {
        private final static DatadictSupport INSTANCE = new DatadictSupport();
    }

    public static DatadictSupport getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private Map<String, Datadict> datadicts;

    public Datadict getDatadictById(String datadictId) {
        Datadict datadict = null;

        if (null == datadicts) {
            datadicts = new HashMap<String, Datadict>();
        }

        if (!datadicts.containsKey(datadictId)) {
            DatadictService datadictService = (DatadictService) ContextHolder
                    .getBean("ws_datadict_datadictService");
            Datadict datadict2 = datadictService.getDatadictById(datadictId);
            if (null != datadict2) {
                datadicts.put(datadictId, datadict2);
                datadict = datadicts.get(datadictId);
            }
        } else {
            datadict = datadicts.get(datadictId);
        }

        return datadict;
    }

}
